import axios from 'axios'
import storage from 'store'
const token = storage.get('token')

export const exportFile = (url, data, fileName = '导出.xlsx', type = 'application/vnd.ms-excel') => {
  return new Promise(async (resolve, reject) => {
    const response = await axios({
      url: process.env.VUE_APP_API_BASE_URL + url,
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${token}`
      },
      responseType: 'blob', // 设置响应类型为二进制
      params: data
    });
    if (response.data) {
      resolve(true)
    } else {
      reject(false)
    }
    const blob = new Blob([response.data], { type: type });
    const href = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = href;
    a.download = fileName; // 导出文件名
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  })

} 